/*************************
* AUDIO STUFF STARTS HERE*
*************************/

module audio3(
  // Clock Input (50 MHz)
  input CLOCK_50, // 50 MHz
  input TD_CLK27, // 27 MHz
  //  Push Buttons
  input  [3:0]  KEY,
  //  DPDT Switches 
  input  [17:0]  SW,
  //  LEDs
  output  [8:0]  LEDG,  //  LED Green[8:0]
  output  [17:0]  LEDR, //  LED Red[17:0]
  // TV Decoder
  output TD_RESET_N, // TV Decoder Reset
  // I2C
  inout  I2C_SDAT, // I2C Data
  output I2C_SCLK, // I2C Clock
  // Audio CODEC
  output/*inout*/ AUD_ADCLRCK, // Audio CODEC ADC LR Clock
  input	 AUD_ADCDAT,  // Audio CODEC ADC Data
  output /*inout*/  AUD_DACLRCK, // Audio CODEC DAC LR Clock
  output AUD_DACDAT,  // Audio CODEC DAC Data
  inout	 AUD_BCLK,    // Audio CODEC Bit-Stream Clock
  output AUD_XCK,     // Audio CODEC Chip Clock
  output  [6:0]  HEX0, HEX1, HEX2, HEX3, HEX4, HEX5, HEX6, HEX7,
  input	PS2_DAT,
  input	PS2_CLK,
  inout  [35:0]  GPIO_0, GPIO_1
);

  

wire tone;

wire [6:0] myclock;
wire RST;
assign RST = KEY[0];

// reset delay gives some time for peripherals to initialize
wire DLY_RST;
Reset_Delay r0(	.iCLK(CLOCK_50),.oRESET(DLY_RST) );

assign	TD_RESET_N = 1'b1;  // Enable 27 MHz

VGA_Audio_PLL 	p1 (	
	.areset(~DLY_RST),
	.inclk0(TD_CLK27),
	.c0(VGA_CTRL_CLK),
	.c1(AUD_CTRL_CLK),
	.c2(VGA_CLK)
);

I2C_AV_Config u3(	
//	Host Side
  .iCLK(CLOCK_50),
  .iRST_N(KEY[0]),
//	I2C Side
  .I2C_SCLK(I2C_SCLK),
  .I2C_SDAT(I2C_SDAT)	
);

assign	AUD_ADCLRCK	=	AUD_DACLRCK;
assign	AUD_XCK		=	AUD_CTRL_CLK;

audio_clock u4(	
//	Audio Side
   .oAUD_BCK(AUD_BCLK),
   .oAUD_LRCK(AUD_DACLRCK),
//	Control Signals
  .iCLK_18_4(AUD_CTRL_CLK),
   .iRST_N(DLY_RST)	
);

audio_converter u5(
	// Audio side
	.AUD_BCK(AUD_BCLK),       // Audio bit clock
	.AUD_LRCK(AUD_DACLRCK), // left-right clock
	.AUD_ADCDAT(AUD_ADCDAT),
	.AUD_DATA(AUD_DACDAT),
	// Controller side
	.iRST_N(DLY_RST),  // reset
	.AUD_outL(audio_outL),
	.AUD_outR(audio_outR),
	.AUD_inL(audio_inL),
	.AUD_inR(audio_inR)
);

wire [15:0] audio_inL, audio_inR;
wire [15:0] audio_outL, audio_outR;
wire [15:0] signal;

//set up DDS frequency
//Use switches to set freq
wire [31:0] dds_incrR;
wire [31:0] dds_incrL;
//wire [31:0] freq = SW[3:0]+10*SW[7:4]+100*SW[11:8]+1000*SW[15:12]+10000*SW[17:16];

wire [31:0] freqR = 32'd750;  //Right Sound Frequency in Hz
wire [31:0] freqL = 32'd750;	//Left Sound Frequency in Hz

assign dds_incrR = freqR * 91626 ; //91626 = 2^32/46875 so SW is in Hz
assign dds_incrL = freqL * 91626 ; //91626 = 2^32/46875 so SW is in Hz

reg [31:0] dds_phaseR;
reg [31:0] dds_phaseL;

always @(negedge AUD_DACLRCK or negedge DLY_RST)
	if (!DLY_RST) dds_phaseR <= 0;
	else dds_phaseR <= dds_phaseR + dds_incrR;

always @(negedge AUD_DACLRCK or negedge DLY_RST)
	if (!DLY_RST) dds_phaseL <= 0;
	else dds_phaseL <= dds_phaseL + dds_incrL;	
	
	
wire [7:0] indexR = dds_phaseR[31:24];
wire [7:0] indexL = dds_phaseL[31:24];


wire [15:0] ONaudio_outR;
wire [15:0] ONaudio_outL;
 
sine_table sigR(
	.index(indexR),
	.signal(ONaudio_outR)
);

sine_table sigL(
	.index(indexL),
	.signal(ONaudio_outL)
);


	//audio_outR <= audio_inR;

//always @(posedge AUD_DACLRCK)
//assign audio_outL = 15'h0000;

assign audio_outR = onR ? ONaudio_outR : 15'h0000;
assign audio_outL = onL ? ONaudio_outL : 15'h0000;

assign LEDG[7] = tone;

reg [25:0] count;
reg onR, onL;

always @ (posedge CLOCK_50 or negedge SW[0]) begin
	if(SW[0] == 1'b0) begin
	end
	else if (CLOCK_50 == 1'b1) begin
		if(tone == 1'b1)begin
			count <= count + 1'b1;
			if(count == 26'd50000000) begin
				count <= 26'd0;
			end
			else if(count < 26'd50000000) begin
				onR <= 1'b1;
				onL <= 1'b1;
			end
		end
		else begin
			onR <= 1'b0;
			onL <= 1'b0;			
		end
	end

end

wire [7:0] keycode;
wire [7:0] keybreak;

ps2Keyboard(CLOCK_50, KEY, SW, HEX0, HEX1, HEX2, HEX3, HEX4, HEX5, HEX6, HEX7, PS2_DAT, PS2_CLK, GPIO_0, GPIO_1, keycode, keybreak);
morseIOhandler handler(CLOCK_50, SW[0], keycode, keybreak, tone, LEDG[0], LEDG[1], LEDR[17:13]);

endmodule


/*****************************
* KEYBOARD STUFF STARTS HERE*
*****************************/

module ps2Keyboard(
  // Clock Input (50 MHz)
  input  CLOCK_50,
  //  Push Buttons
  input  [3:0]  KEY,
  //  DPDT Switches 
  input  [17:0]  SW,
  //  7-SEG Displays
  output  [6:0]  HEX0, HEX1, HEX2, HEX3, HEX4, HEX5, HEX6, HEX7,
  //  PS2 data and clock lines		
  input	PS2_DAT,
  input	PS2_CLK,
  //  GPIO Connections
  inout  [35:0]  GPIO_0, GPIO_1,
  output [7:0] keycode,
  output [7:0] keybreak
);

wire [7:0] scan_code;
//  set all inout ports to tri-state
assign  GPIO_0    =  36'hzzzzzzzzz;
assign  GPIO_1    =  36'hzzzzzzzzz;

assign keybreak = history[2];
assign keycode = history[1];

wire RST;
assign RST = KEY[0];


wire reset = 1'b0;


reg [7:0] history[1:4];
wire read, scan_ready;

oneshot pulser(
   .pulse_out(read),
   .trigger_in(scan_ready),
   .clk(CLOCK_50)
);

keyboard kbd(
  .keyboard_clk(PS2_CLK),
  .keyboard_data(PS2_DAT),
  .clock50(CLOCK_50),
  .reset(reset),
  .read(read),
  .scan_ready(scan_ready),
  .scan_code(scan_code)
);

hex_7seg dsp0(scan_code[3:0],HEX0);
hex_7seg dsp1(scan_code[7:4],HEX1);

hex_7seg dsp2(history[2][3:0],HEX2);
hex_7seg dsp3(history[2][7:4],HEX3);

hex_7seg dsp4(history[3][3:0],HEX4);
hex_7seg dsp5(history[3][7:4],HEX5);

hex_7seg dsp6(history[4][3:0],HEX6);
hex_7seg dsp7(history[4][7:4],HEX7);



always @(posedge scan_ready)
begin
	history[4] <= history[3];
	history[3] <= history[2];
	history[2] <= history[1];
	history[1] <= scan_code;
end
	

// blank remaining digits
/*
wire [6:0] blank = 7'h7f;
assign HEX2 = blank;
assign HEX3 = blank;
assign HEX4 = blank;
assign HEX5 = blank;
assign HEX6 = blank;
assign HEX7 = blank;
*/

endmodule



/******************************
* MORSE CODE STUFF STARTS HERE*
******************************/



module morseIOhandler(clk, rst, key, keybreak, tone, rstlight, dangerlight, dot_counter);
input clk, rst;
input [7:0] key, keybreak;
//input[5:0] character;
//output[5:0] character_set;
output tone, rstlight, dangerlight;
output [4:0] dot_counter;
reg rstcount, tone, dangerlight, rstlight, add_dot, rstdot_counter;
reg [4:0] dot_counter;
reg [31:0] count;
reg [7:0] S, NS, character_set, character;

parameter [7:0] 	KEY_A = 8'h1C,
						KEY_B = 8'h32,
						KEY_C = 8'h21,
						KEY_D = 8'h23,
						KEY_E = 8'h24,
						KEY_F = 8'h2B,
						KEY_G = 8'h34,
						KEY_H = 8'h33,
						KEY_I = 8'h43,
						KEY_J = 8'h3B,
						KEY_K = 8'h42,
						KEY_L = 8'h4B,
						KEY_M = 8'h3A,
						KEY_N = 8'h31,
						KEY_O = 8'h44,
						KEY_P = 8'h4D,
						KEY_Q = 8'h15,
						KEY_R = 8'h2D,
						KEY_S = 8'h1B,
						KEY_T = 8'h2C,
						KEY_U = 8'h3C,
						KEY_V = 8'h2A,
						KEY_W = 8'h1D,
						KEY_X = 8'h22,
						KEY_Y = 8'h35,
						KEY_Z = 8'h1A,
						KEY_0 = 8'h45,
						KEY_1 = 8'h16,
						KEY_2 = 8'h1E,
						KEY_3 = 8'h26,
						KEY_4 = 8'h25,
						KEY_5 = 8'h2E,
						KEY_6 = 8'h36,
						KEY_7 = 8'h3D,
						KEY_8 = 8'h3E,
						KEY_9 = 8'h46,
						STANDBY = 8'd0,
						DANGER = 8'd1,
						DOT = 8'd2,
						DASH = 8'd3,
						PAUSE = 8'd4,
						END = 8'd5;

parameter [31:0] 	DOT_LENGTH = 31'd6250000;
						
always@(posedge clk or negedge rst)
begin
	if(rst == 1'b0)
	begin
		S <= STANDBY;
	end
	else
	begin
		S <= NS;
		if(rstcount == 1'b1)
			begin
			count <= 32'b0;
			if(rstdot_counter == 1'b0)
				dot_counter <= dot_counter + 5'd1;
			else
				dot_counter <= 5'd0;
			end
		else 
			begin
			count <= count + 1;
			dot_counter <= dot_counter;
			end
	end
end

always@(*)
begin
		case(S)
			DOT: begin
			rstdot_counter = 1'b0;
			if(count < DOT_LENGTH)begin
				NS = DOT;
				rstcount = 1'b0;
				end
			else begin
				//dot_counter = dot_counter + 5'd1;
				rstcount = 1'b1;
				case(key)
					KEY_A: NS = KEY_A;
					KEY_B: NS = KEY_B;
					KEY_C: NS = KEY_C;
					KEY_D: NS = KEY_D;
					KEY_E: NS = KEY_E;
					KEY_F: NS = KEY_F;
					KEY_G: NS = KEY_G;
					KEY_H: NS = KEY_H;
					KEY_I: NS = KEY_I;
					KEY_J: NS = KEY_J;
					KEY_K: NS = KEY_K;
					KEY_L: NS = KEY_L;
					KEY_M: NS = KEY_M;
					KEY_N: NS = KEY_N;
					KEY_O: NS = KEY_O;
					KEY_P: NS = KEY_P;
					KEY_Q: NS = KEY_Q;
					KEY_R: NS = KEY_R;
					KEY_S: NS = KEY_S;
					KEY_T: NS = KEY_T;
					KEY_U: NS = KEY_U;
					KEY_V: NS = KEY_V;
					KEY_W: NS = KEY_W;
					KEY_X: NS = KEY_X;
					KEY_Y: NS = KEY_Y;
					KEY_Z: NS = KEY_Z;
					KEY_1: NS = KEY_1;
					KEY_2: NS = KEY_2;
					KEY_3: NS = KEY_3;
					KEY_4: NS = KEY_4;
					KEY_5: NS = KEY_5;
					KEY_6: NS = KEY_6;
					KEY_7: NS = KEY_7;
					KEY_8: NS = KEY_8;
					KEY_9: NS = KEY_9;
					KEY_0: NS = KEY_0;
					default: NS = STANDBY;
					endcase
					end
			end
			
			DASH: begin
			rstdot_counter = 1'b0;
			if(count < DOT_LENGTH * 3)begin
				NS = DASH;
				rstcount = 1'b0;
				end
			else begin
				//dot_counter = dot_counter + 5'd1;
				rstcount = 1'b1;
				case(key)
					KEY_A: NS = KEY_A;
					KEY_B: NS = KEY_B;
					KEY_C: NS = KEY_C;
					KEY_D: NS = KEY_D;
					KEY_E: NS = KEY_E;
					KEY_F: NS = KEY_F;
					KEY_G: NS = KEY_G;
					KEY_H: NS = KEY_H;
					KEY_I: NS = KEY_I;
					KEY_J: NS = KEY_J;
					KEY_K: NS = KEY_K;
					KEY_L: NS = KEY_L;
					KEY_M: NS = KEY_M;
					KEY_N: NS = KEY_N;
					KEY_O: NS = KEY_O;
					KEY_P: NS = KEY_P;
					KEY_Q: NS = KEY_Q;
					KEY_R: NS = KEY_R;
					KEY_S: NS = KEY_S;
					KEY_T: NS = KEY_T;
					KEY_U: NS = KEY_U;
					KEY_V: NS = KEY_V;
					KEY_W: NS = KEY_W;
					KEY_X: NS = KEY_X;
					KEY_Y: NS = KEY_Y;
					KEY_Z: NS = KEY_Z;
					KEY_1: NS = KEY_1;
					KEY_2: NS = KEY_2;
					KEY_3: NS = KEY_3;
					KEY_4: NS = KEY_4;
					KEY_5: NS = KEY_5;
					KEY_6: NS = KEY_6;
					KEY_7: NS = KEY_7;
					KEY_8: NS = KEY_8;
					KEY_9: NS = KEY_9;
					KEY_0: NS = KEY_0;
					default: NS = STANDBY;
					endcase
					end
			end
			
			PAUSE: begin
			rstdot_counter = 1'b0;
			if(count < DOT_LENGTH)begin
				NS = PAUSE;
				rstcount = 1'b0;
				end
			else begin
				//dot_counter = dot_counter + 5'd1;
				rstcount = 1'b1;
				case(key)
					KEY_A: NS = KEY_A;
					KEY_B: NS = KEY_B;
					KEY_C: NS = KEY_C;
					KEY_D: NS = KEY_D;
					KEY_E: NS = KEY_E;
					KEY_F: NS = KEY_F;
					KEY_G: NS = KEY_G;
					KEY_H: NS = KEY_H;
					KEY_I: NS = KEY_I;
					KEY_J: NS = KEY_J;
					KEY_K: NS = KEY_K;
					KEY_L: NS = KEY_L;
					KEY_M: NS = KEY_M;
					KEY_N: NS = KEY_N;
					KEY_O: NS = KEY_O;
					KEY_P: NS = KEY_P;
					KEY_Q: NS = KEY_Q;
					KEY_R: NS = KEY_R;
					KEY_S: NS = KEY_S;
					KEY_T: NS = KEY_T;
					KEY_U: NS = KEY_U;
					KEY_V: NS = KEY_V;
					KEY_W: NS = KEY_W;
					KEY_X: NS = KEY_X;
					KEY_Y: NS = KEY_Y;
					KEY_Z: NS = KEY_Z;
					KEY_1: NS = KEY_1;
					KEY_2: NS = KEY_2;
					KEY_3: NS = KEY_3;
					KEY_4: NS = KEY_4;
					KEY_5: NS = KEY_5;
					KEY_6: NS = KEY_6;
					KEY_7: NS = KEY_7;
					KEY_8: NS = KEY_8;
					KEY_9: NS = KEY_9;
					KEY_0: NS = KEY_0;
					default: NS = STANDBY;
					endcase
				end
			end
			
			END: begin
			if(count < DOT_LENGTH * 3)begin
				NS = END;
				rstcount = 1'b0;
				rstdot_counter = 1'b0;
				end
			else begin
				rstdot_counter = 1'b1;
				rstcount = 1'b1;
				NS = STANDBY;
					end
			end
		
			KEY_A:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = PAUSE;
					5'd2: NS = DASH;
					5'd3: NS = END;
					default: NS = KEY_A;
				endcase
			end
			KEY_B:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = PAUSE;
					5'd4: NS = DOT;
					5'd5: NS = PAUSE;
					5'd6: NS = DOT;
					5'd7: NS = END;
					default: NS = KEY_B;
				endcase
			end
			KEY_C:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = PAUSE;
					5'd4: NS = DASH;
					5'd5: NS = PAUSE;
					5'd6: NS = DOT;
					5'd7: NS = END;
					default: NS = KEY_C;
				endcase
			end
			KEY_D:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = PAUSE;
					5'd4: NS = DOT;
					5'd5: NS = END;
					default: NS = KEY_D;
				endcase
			end
			KEY_E:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = END;
					default: NS = KEY_E;
				endcase
			end
			KEY_F:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = PAUSE;
					5'd4: NS = DASH;
					5'd5: NS = PAUSE;
					5'd6: NS = DOT;
					5'd7: NS = END;
					default: NS = KEY_F;
				endcase
			end
			KEY_G:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = PAUSE;
					5'd2: NS = DASH;
					5'd3: NS = PAUSE;
					5'd4: NS = DOT;
					5'd5: NS = END;
					default: NS = KEY_G;
				endcase
			end
			KEY_H:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = PAUSE;
					5'd4: NS = DOT;
					5'd5: NS = PAUSE;
					5'd6: NS = DOT;
					5'd7: NS = END;
					default: NS = KEY_H;
				endcase
			end
			KEY_I:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = END;
					default: NS = KEY_I;
				endcase
			end
			KEY_J:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = PAUSE;
					5'd2: NS = DASH;
					5'd3: NS = PAUSE;
					5'd4: NS = DASH;
					5'd5: NS = PAUSE;
					5'd6: NS = DASH;
					5'd7: NS = END;
					default: NS = KEY_J;
				endcase
			end
			KEY_K:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = PAUSE;
					5'd4: NS = DASH;
					5'd5: NS = PAUSE;
					5'd6: NS = END;
					default: NS = KEY_K;
				endcase
			end
			KEY_L:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = PAUSE;
					5'd2: NS = DASH;
					5'd3: NS = PAUSE;
					5'd4: NS = DOT;
					5'd5: NS = PAUSE;
					5'd6: NS = DOT;
					5'd7: NS = END;
					default: NS = KEY_L;
				endcase
			end
			KEY_M:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = PAUSE;
					5'd2: NS = DASH;
					5'd3: NS = END;
					default: NS = KEY_M;
				endcase
			end
			KEY_N:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = END;
					default: NS = KEY_N;
				endcase
			end
			KEY_O:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = PAUSE;
					5'd2: NS = DASH;
					5'd3: NS = PAUSE;
					5'd4: NS = DASH;
					5'd5: NS = END;
					default: NS = KEY_O;
				endcase
			end
			KEY_P:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = PAUSE;
					5'd2: NS = DASH;
					5'd3: NS = PAUSE;
					5'd4: NS = DASH;
					5'd5: NS = PAUSE;
					5'd6: NS = DOT;
					5'd7: NS = END;
					default: NS = KEY_P;
				endcase
			end
			KEY_Q:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = PAUSE;
					5'd2: NS = DASH;
					5'd3: NS = PAUSE;
					5'd4: NS = DOT;
					5'd5: NS = PAUSE;
					5'd6: NS = DASH;
					5'd7: NS = END;
					default: NS = KEY_Q;
				endcase
			end
			KEY_R:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = PAUSE;
					5'd2: NS = DASH;
					5'd3: NS = PAUSE;
					5'd4: NS = DOT;
					5'd5: NS = END;
					default: NS = KEY_R;
				endcase
			end
			KEY_S:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = PAUSE;
					5'd4: NS = DOT;
					5'd5: NS = END;
					default: NS = KEY_S;
				endcase
			end
			KEY_T:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = END;
					default: NS = KEY_T;
				endcase
			end
			KEY_U:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = PAUSE;
					5'd4: NS = DASH;
					5'd5: NS = END;
					default: NS = KEY_U;
				endcase
			end
			KEY_V:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = PAUSE;
					5'd4: NS = DOT;
					5'd5: NS = PAUSE;
					5'd6: NS = DASH;
					5'd7: NS = END;
					default: NS = KEY_V;
				endcase
			end
			KEY_W:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = PAUSE;
					5'd2: NS = DASH;
					5'd3: NS = PAUSE;
					5'd4: NS = DASH;
					5'd5: NS = END;
					default: NS = KEY_W;
				endcase
			end
			KEY_X:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = PAUSE;
					5'd4: NS = DOT;
					5'd5: NS = PAUSE;
					5'd6: NS = DASH;
					5'd7: NS = END;
					default: NS = KEY_X;
				endcase
			end
			KEY_Y:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = PAUSE;
					5'd4: NS = DASH;
					5'd5: NS = PAUSE;
					5'd6: NS = DASH;
					5'd7: NS = END;
					default: NS = KEY_Y;
				endcase
			end
			KEY_Z:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = PAUSE;
					5'd2: NS = DASH;
					5'd3: NS = PAUSE;
					5'd4: NS = DOT;
					5'd5: NS = PAUSE;
					5'd6: NS = DOT;
					5'd7: NS = END;
					default: NS = KEY_Z;
				endcase
			end
			KEY_0:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = PAUSE;
					5'd2: NS = DASH;
					5'd3: NS = PAUSE;
					5'd4: NS = DASH;
					5'd5: NS = PAUSE;
					5'd6: NS = DASH;
					5'd7: NS = PAUSE;
					5'd8: NS = DASH;
					5'd9: NS = END;
					default: NS = KEY_0;
				endcase
			end
			KEY_1:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = PAUSE;
					5'd2: NS = DASH;
					5'd3: NS = PAUSE;
					5'd4: NS = DASH;
					5'd5: NS = PAUSE;
					5'd6: NS = DASH;
					5'd7: NS = PAUSE;
					5'd8: NS = DASH;
					5'd9: NS = END;
					default: NS = KEY_1;
				endcase
			end
			KEY_2:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = PAUSE;
					5'd4: NS = DASH;
					5'd5: NS = PAUSE;
					5'd6: NS = DASH;
					5'd7: NS = PAUSE;
					5'd8: NS = DASH;
					5'd9: NS = END;
					default: NS = KEY_2;
				endcase
			end
			KEY_3:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = PAUSE;
					5'd4: NS = DOT;
					5'd5: NS = PAUSE;
					5'd6: NS = DASH;
					5'd7: NS = PAUSE;
					5'd8: NS = DASH;
					5'd9: NS = END;
					default: NS = KEY_3;
				endcase
			end
			KEY_4:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = PAUSE;
					5'd4: NS = DOT;
					5'd5: NS = PAUSE;
					5'd6: NS = DOT;
					5'd7: NS = PAUSE;
					5'd8: NS = DASH;
					5'd9: NS = END;
					default: NS = KEY_4;
				endcase
			end
			KEY_5:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DOT;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = PAUSE;
					5'd4: NS = DOT;
					5'd5: NS = PAUSE;
					5'd6: NS = DOT;
					5'd7: NS = PAUSE;
					5'd8: NS = DOT;
					5'd9: NS = END;
					default: NS = KEY_5;
				endcase
			end
			KEY_6:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = PAUSE;
					5'd2: NS = DOT;
					5'd3: NS = PAUSE;
					5'd4: NS = DOT;
					5'd5: NS = PAUSE;
					5'd6: NS = DOT;
					5'd7: NS = PAUSE;
					5'd8: NS = DOT;
					5'd9: NS = END;
					default: NS = KEY_6;
				endcase
			end
			KEY_7:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = PAUSE;
					5'd2: NS = DASH;
					5'd3: NS = PAUSE;
					5'd4: NS = DOT;
					5'd5: NS = PAUSE;
					5'd6: NS = DOT;
					5'd7: NS = PAUSE;
					5'd8: NS = DOT;
					5'd9: NS = END;
					default: NS = KEY_7;
				endcase
			end
			KEY_8:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = PAUSE;
					5'd2: NS = DASH;
					5'd3: NS = PAUSE;
					5'd4: NS = DASH;
					5'd5: NS = PAUSE;
					5'd6: NS = DOT;
					5'd7: NS = PAUSE;
					5'd8: NS = DOT;
					5'd9: NS = END;
					default: NS = KEY_8;
				endcase
			end
			KEY_9:begin
			rstcount = 1'b0;
			rstdot_counter = 1'b0;
				case(dot_counter)
					5'd0: NS = DASH;
					5'd1: NS = PAUSE;
					5'd2: NS = DASH;
					5'd3: NS = PAUSE;
					5'd4: NS = DASH;
					5'd5: NS = PAUSE;
					5'd6: NS = DASH;
					5'd7: NS = PAUSE;
					5'd8: NS = DOT;
					5'd9: NS = END;
					default: NS = KEY_9;
				endcase
			end
			STANDBY:begin
			rstdot_counter = 1'b1;
			rstcount = 1'b1;
			if(keybreak != 8'hF0)
				case(key)
					KEY_A: NS = KEY_A;
					KEY_B: NS = KEY_B;
					KEY_C: NS = KEY_C;
					KEY_D: NS = KEY_D;
					KEY_E: NS = KEY_E;
					KEY_F: NS = KEY_F;
					KEY_G: NS = KEY_G;
					KEY_H: NS = KEY_H;
					KEY_I: NS = KEY_I;
					KEY_J: NS = KEY_J;
					KEY_K: NS = KEY_K;
					KEY_L: NS = KEY_L;
					KEY_M: NS = KEY_M;
					KEY_N: NS = KEY_N;
					KEY_O: NS = KEY_O;
					KEY_P: NS = KEY_P;
					KEY_Q: NS = KEY_Q;
					KEY_R: NS = KEY_R;
					KEY_S: NS = KEY_S;
					KEY_T: NS = KEY_T;
					KEY_U: NS = KEY_U;
					KEY_V: NS = KEY_V;
					KEY_W: NS = KEY_W;
					KEY_X: NS = KEY_X;
					KEY_Y: NS = KEY_Y;
					KEY_Z: NS = KEY_Z;
					KEY_1: NS = KEY_1;
					KEY_2: NS = KEY_2;
					KEY_3: NS = KEY_3;
					KEY_4: NS = KEY_4;
					KEY_5: NS = KEY_5;
					KEY_6: NS = KEY_6;
					KEY_7: NS = KEY_7;
					KEY_8: NS = KEY_8;
					KEY_9: NS = KEY_9;
					KEY_0: NS = KEY_0;
					default: NS = STANDBY;
					endcase
				else
					NS = STANDBY;
			end
			default:begin
			rstdot_counter = 1'b1;
			rstcount = 1'b1;
			NS = DANGER;
			end
			endcase
end
always@(posedge clk or negedge rst)
begin
	
	if(rst == 1'b0)
	begin
		tone <= 1'b0;
		rstlight <= 1'b1;
		dangerlight <= 1'b0;
	end
	else
	
	begin
		rstlight <= 1'b0;
		case(S)
				KEY_A: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_B: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_C: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_D: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_E: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_F: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_G: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_H: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_I: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_J: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_K: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_L: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_M: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_N: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_O: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_P: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_Q: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_R: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_S: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_T: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_U: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_V: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_W: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_X: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_Y: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_Z: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_1: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_2: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_3: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_4: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_5: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_6: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_7: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_8: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_9: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				KEY_0: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				DOT: begin tone <=1'b1;
				dangerlight <= 1'b0;
				end
				DASH: begin tone <=1'b1;
				dangerlight <= 1'b0;
				end
				PAUSE: begin tone <=1'b0;
				dangerlight <= 1'b0;
				end
				END: begin tone <=1'b0;
				dangerlight <= 1'b0;
				end
				STANDBY: begin tone <= 1'b0;
				dangerlight <= 1'b0;
				end
				DANGER: begin dangerlight <= 1'b1;
				tone <= 1'b0;
				end
				default: begin dangerlight <= 1'b1;
				tone <= 1'b0;
				end
				endcase
		end
		end
endmodule 